package com.zsr.system.service.zfb.impl;

import com.alibaba.fastjson2.JSONObject;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;

import com.zsr.common.utils.DateUtils;
import com.zsr.common.utils.StringUtils;
import com.zsr.system.service.zfb.IAliPayBusinessService;
import com.zsr.system.service.zfb.IAliPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author 荣
 * @date 2025/1/9 14:20
 */
@Service
@Slf4j
public class AliPayBusinessServiceImpl implements IAliPayBusinessService {

    @Resource
    private IAliPayService aliPayService;

    public static void main(String[] args) {
        String time = "2025-01-09 18:25:23";
        Date date = DateUtils.parseDate(time);

    }

    @Override
    public JSONObject transferService(JSONObject jsonObject, String title, String remark) {
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
        // 设置商家侧唯一订单号
        model.setOutBizNo(jsonObject.getString("orderId"));
        // 设置订单总金额
        model.setTransAmount(jsonObject.getString("money"));
        // 设置描述特定的业务场景 单笔无密转账:DIRECT_TRANSFER
        model.setBizScene("DIRECT_TRANSFER");
        // 设置业务产品码 单笔无密转账:TRANS_ACCOUNT_NO_PWD
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        // 设置转账业务的标题
        model.setOrderTitle(StringUtils.isEmpty(title)? "红包发放" : title);
        // 设置收款方信息
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        payeeInfo.setIdentity(jsonObject.getString("phoneNumber"));
        payeeInfo.setName(jsonObject.getString("readName"));
        model.setPayeeInfo(payeeInfo);
        // 设置业务备注
        model.setRemark(StringUtils.isEmpty(remark)? "iKF活动红包发放备注" : remark);

        // 设置转账业务请求的扩展参数
//        model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");

        AlipayConfig alipayConfig = new AlipayConfig();
        // 测试环境应用私钥
//        alipayConfig.setPrivateKey("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC3ImnNglu8WbOjAqRtlKosEXG78VVLL3MZG2/rd0MjnFHU31vqWyiUgZfPyEg06XgKR8eGGSj4X+SYL6nGDFWUqYIskbG33XgQvvqb0+bykHUPuqenXycxEdnUExkh6Ih6CzPuEW1bXtnY8Yf/2tmR8D7HRhuQU21be1iGjWct7CTc4TgOQaX9u7vZPA6c6wuqQR525ZfgTkDTJ/SRRLZavQAFBq5IBkFtRdPsOEEY4bdNOV52qPkr0Lt9WL0KtYw9Ip/IgBbqbeNf3Pu6ZcjDxZK0LjSsdCs1LeaAbhFpelsCyWx0F330dxoIcQr/DKLOFVyoAXv5ctY43Qmc9SwvAgMBAAECggEBAJBlZ/sMX/yg1c0gFjn40Dyy/HRuEwyXOy9+vL5yaNO5O6SXAMhGXDbL/8Frb9OYinfApQeM6wr7wShl3H012HzBMsyli/UngLXjf4JCkISNdl+SkwKbOjGsm45mCJj92i4wix+ZShqTLT2rI8qO6qHxRe6lzrH7tew90000TTic9LDX+xQ3TAVu8IRSEzJ/NH08tsUfLmvIiTQfazgp6fATncPprD0kkSWEN9WGtABjbrNEe4YGpfR8PRB4qGDvvvcr0fPohDZT7G9ymmHAIXiSaNQXK+bXTV7VuiV4Wu57aXjM3IG6FeOhmhTRZSMy6wn49i/ACCtDt61eAjswTcECgYEA7BrufTOIcWlJCF5p+XYZ7tBmbkqxljhf65OJ3Nz0GgXzDbluFNXCPfrWyvOKb0uZyeSKcIkw9UHjgLrOWs1Fg57935Ab6YvaKmG0hmdmfoGQQ6mnnFMSSnMv4fEJjsj91zCf8OKID4wNWLtYshVOjtNcH+d7HhK9DGHoOxxyRNECgYEAxpDW5eAITUwh+okgECev0ObjOBrkxbTG+3x4l9IDpxJixb15gamW55WkFk8n3K+He8mM9b4i2pIjcJ4mG5sepcnBgQldRUOWE7dPCr07LfUF/WT8/73s7fmIV6KSkmMGD9BY4Jw3+Q5CoPVer7Thr+vXYyw8kgWvFfWkzKIgoP8CgYBVmhoXmKSCgeqxtxFzGaejVzmcCUbDsSLN1YspYY67FKcftbg8o5euQxC3On9ZhK54bXzn+kWe1km2tYP1lfD7HJzs/mGwVj0+GniKNlkJys3USmFzowbBqWuV3n9ZnnOyHnYOrwMnp9566knpHyKBVA5Vnuaba63RHHpHXDPt0QKBgQCPib4++B8oOWPKpvuNkhLPMUiJky9TF+ZLf2PxY7iOOoU6Kki+iWhzykGOevP23NBUlJMYRbUOSVvjGf+0LwMNl3YRIELTQhMn93fk7HvuMdR9o1YcTSALuzlrGDNaLMkB/3twCp2PpVZW4mjrHReUQyk1IJI8gG1bs3HJF1384wKBgCbtTjW6Z5DPOxbDW51VWEFdPtw+w2Q9d5WmbAlbhchbJDj5IHIfrUI68YTbKbkWXM7WGm/TGHP/oCO6JFDNTO0zqls6UvKSqLkfcC5gAz/j58cfZMtNG3pE/tCJoRUZ5c6jBROvKgHzglv9yCQyFJQi4fO+k5eBO+3tbLG54/Z1");
//        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do"); // 沙箱环境地址

        // 正式环境
//        alipayConfig.setPrivateKey("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCLNpjlDAew3KySlGblavt7AmufNkNf9KZ/s2kPy43/qWc3By37poGWS/cSHRqlBXUUflG1+jzRFAZFL2skHN9yTHkg014UR7sgbf83u/wtlnSMkuLzIHB4fZvQBqZ+wNqrB9QTw/v2Pq84EKS4yvgB18SUDg3/sfF2IOcMJwS6WQ0qQ1LbyWd3zaL3m8eKhJDsYObgoSeL7gzum5R7Pp+vFgCVdEFchriabs1CAfqtmgHfmwAN5Iwe28xdnUu9F8ft04H/WkQBGX0pfeKkzD99F+orvVs+ogldnl6+iLbKbowTyPVCmelu3tPA3tifFNWNt0MJrDF1pg0pt7lx3hQfAgMBAAECggEARHikFvQlzD2du7K2MVy/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qqbOsE5Ydvg15X3qQXXSe6Hiw3FQ/5NPeRDQXnaQ+q1bNFhBqbyNNf6CzZe4OVe7w/yT0oaU9A0r2FaccrYE3M1jSr+POkQ8+fx15e2wNtn/Itn11RKkBtRDkBKF8LiTC7SW");
//        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");

        // 测试环境APPId
//        alipayConfig.setAppId("9021000137629130");
        // 正式环境APPId
//        alipayConfig.setAppId("2021005111654822");
//        alipayConfig.setCharset("UTF-8");
//        alipayConfig.setSignType("RSA2");
//        alipayConfig.setFormat("json");

        // 测试环境证书地址
//        alipayConfig.setAppCertPath("/home/java/javajar/zfb/appPublicCert.crt");
//        alipayConfig.setAlipayPublicCertPath("/home/java/javajar/zfb/alipayPublicCert.crt");
//        alipayConfig.setRootCertPath("/home/java/javajar/zfb/alipayRootCert.crt");

        // 正式环境证书地址
//        alipayConfig.setAppCertPath("/home/local/java/javajar/zfb/appCertPublicKey_2021005111654822.crt");
//        alipayConfig.setAlipayPublicCertPath("/home/local/java/javajar/zfb/alipayCertPublicKey_RSA2.crt");
//        alipayConfig.setRootCertPath("/home/local/java/javajar/zfb/alipayRootCert.crt");

        AlipayFundTransUniTransferResponse response = aliPayService.transfer(model, alipayConfig);
        if (response.isSuccess()) {
            String orderId = response.getOutBizNo();
            log.info("支付宝转账成功，支付宝转账订单号：{}", orderId);
            if (orderId.equals(jsonObject.getString("orderId"))) {

                // 业务操作
                log.info("转账操作成功");

            } else {
                log.info("系统订单号找不到，但是已经成功转账，支付宝订单号为:{}", response.getOrderId());
            }
        } else {
            log.info("支付宝转账失败");

        }
        return jsonObject;
    }


}
